﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using System.Web.Services;
using RN.ERP.Web.BasicService;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseSeniorSearch : BasePage
    {
        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }
        /// <summary>
        /// 登录用户roleid
        /// </summary>
        protected string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        /// <summary>
        /// 当前市场
        /// </summary>
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        public string ClassID
        {
            get
            {
                return Request["ClassID"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindDropDown();
                hdnClassID.Value = ClassID;
                var dic = GetClassIdMatchRoleDic();
                if (dic.ContainsValue(ClassID))
                {
                    hdnRoleId.Value = dic.First(d=>d.Value==ClassID).Key;
                }
                else
                {
                    hdnRoleId.Value = "-1";
                }
                BindFlowCode();
            }
        }

        private void BindDropDown()
        {
            ////企业类型
            //var CorpUserType = JsonConvert.DeserializeObject<Dictionary<string, string>>(Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString());
            //DDLCorpUserType.DataSource = CorpUserType;
            //DDLCorpUserType.DataValueField = "Key";
            //DDLCorpUserType.DataTextField = "Value";
            //DDLCorpUserType.DataBind();
            //DDLCorpUserType.Items.Insert(0, new ListItem("--请选择--", "-1"));
        }

        private Dictionary<string, string> GetClassIdMatchRoleDic()
        {
            RN.ERP.Web.BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            return JsonConvert.DeserializeObject<Dictionary<string,string>>( client.GetDictionary("RoleClassId", string.Empty, string.Empty));
        }


        private void BindFlowCode()
        {
            if (SystemID.Equals("97") || SystemID.Equals("101"))
            {
                BindFlowCodeForYTLC();
                return;
            }

            string classid = Request.QueryString["classid"];
            string msg = string.Empty;
            BasicServiceClient basicServiceClient = new BasicServiceClient();
            var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(basicServiceClient.GetAllFlowStatus(out msg, UserInfo.CurrentArea, UserInfo.RoleId, classid));
            ddlStatus.Items.Clear();
            foreach (var key in dic.Keys)
            {
                ddlStatus.Items.Add(new ListItem(key, dic[key]));
            }
            ddlStatus.Items.Insert(0, new ListItem("全部", ""));
            ddlStatus.SelectedValue = "处理中";
        }

        private void BindFlowCodeForYTLC()
        {
            string msg = string.Empty;
            string classId = Request.QueryString["classid"];
            if (string.IsNullOrEmpty(classId) || classId == "0")
            {
                classId = "2511";
            }

            Dictionary<string, string> list = new Dictionary<string, string>();
            Dictionary<string, string> listNew = new Dictionary<string, string>();
            BasicService.BasicServiceClient client = new BasicService.BasicServiceClient();
            list = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllBusinessStatusAndProcessing(out msg, RoleId, AreaCode, classId, false));

            foreach (var item in list.Keys)
            {
                if (item.Equals("0"))
                {
                    continue;////主管端不显示未上报的
                }
                string key = item;
                if (key.Contains("@"))
                {
                    key = key.Replace('@', '|');

                }
                listNew.Add(key, list[item]);
            }

            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Key";
            ddlStatus.DataSource = listNew;
            ddlStatus.DataBind();

            if (listNew != null && listNew.Count > 1)
            {
                ddlStatus.SelectedIndex = 1;
            }
        }

    }


}